<?php
namespace App\Common\Cla\Rbac\User;

use App\Common as com;
use App\Common\Cla\Rbac\User\Relation as thisRelation;
use App\Common\Cla\Rbac as nameRbac;

/**
 * @property nameRbac\User\DataStore $dataStore
 */
class Relation extends com\Cla\ClaBase\CommonOfId\Client {

	/**
	 * relation constructor.
	 * @param nameRbac\User\DataStore $dataStore
	 */
	public function __construct( nameRbac\User\DataStore $dataStore ) {
		parent::__construct( $dataStore );
	}

	/**
	 * @return com\Model\RbacUserRelation\DataManager
	 */
	public function goDirectsRead(  ):com\Model\RbacUserRelation\DataManager {
	    return $this->goPool( __FUNCTION__, function(){
	        return $this->newDirectsDataManager();
	    } );
	}

	/**
	 * @return com\Model\RbacUserRelation\DataManager
	 */
	public function newDirectsDataManager(  ):com\Model\RbacUserRelation\DataManager {
	    return com\Model\RbacUserRelation::dataManager()->whereEqData_UserType( $this->dataStore->main->getUserType() )
			->whereEqData_UserId( $this->dataStore->main->getUserId() );
	}

	/**
	 * 绑定role
	 * @param array $roleIds
	 * @return bool
	 * @throws com\Lib\Err\BaseException\Throw_
	 * @throws com\Lib\Err\Exception
	 */
	public function bindRoleIds( array $roleIds ):bool {
		$relationDataOfRole = $this->dataStore->main->gowRole()->goDirectDataManaer_New()
			->fieldData_Id()
			->fieldData_RbacIds()
			->findOrNullToArray();
		if( $relationDataOfRole === null ){
			$saveObj = thisRelation\Sava::makeOfCreate(
				$this->dataStore->main->getUserType(),
				$this->dataStore->main->getUserId(),
				com\Model\RbacUserRelation::gosDataUnit()->goVar()->varRbacType_Role,
			);
			$saveObj->goWaitSaveDataManager()->newData_RbacIds()->setOfContinuousIds( $roleIds );
			$saveObj->saveOfCreateReMainId();
		}else{
			$newRoleIds = array_merge( com\Lib\Fun\Array_::toArray( $relationDataOfRole['rbac_ids'], [
				'unique' => true,
				'sort' => 1,
			]), $roleIds );
			//
			$saveObj = thisRelation\Sava::makeOfUpdate( $relationDataOfRole['id'] );
			$saveObj->goWaitSaveDataManager()->newData_RbacIds()->setOfContinuousIds( $newRoleIds );
			$saveObj->saveOfUpdate();
		}
		//
		return true;
	}

	/**
	 * 解绑role
	 * @param array $roleIds
	 * @return bool
	 */
	public function unBindRoleIds( array $roleIds ):bool {
		$relationDataOfRole = $this->dataStore->main->gowRole()->goDirectDataManaer_New()
			->fieldData_Id()
			->fieldData_RbacIds()
			->findOrNullToArray();
		if( $relationDataOfRole !== null ){
			$saveObj = thisRelation\Sava::makeOfUpdate( $relationDataOfRole['id'] );
			$saveObj->goWaitSaveDataManager()->newData_RbacIds()->setOfContinuousIds( array_diff( $relationDataOfRole['rbac_ids'], $roleIds ) );
			$saveObj->saveOfUpdate();
		}
		//
		return true;
	}

	/**
	 * 绑定PermissionIds
	 * @param array $permissionIds
	 * @return bool
	 * @throws com\Lib\Err\BaseException\Throw_
	 * @throws com\Lib\Err\Exception
	 */
	public function bindPermissionIds( array $permissionIds ):bool {
		$relationDataOfPermission = $this->dataStore->main->gowPermission()->goDirectDataManaer_New()
			->fieldData_Id()
			->fieldData_RbacIds()
			->findOrNullToArray();
		if( $relationDataOfPermission === null ){
			$saveObj = thisRelation\Sava::makeOfCreate(
				$this->dataStore->main->getUserType(),
				$this->dataStore->main->getUserId(),
				com\Model\RbacUserRelation::gosDataUnit()->goVar()->varRbacType_Permission,
			);
			$saveObj->goWaitSaveDataManager()->newData_RbacIds()->setOfContinuousIds( $permissionIds );
			$saveObj->saveOfCreateReMainId();
		}else{
			$newPermissionIds = array_merge( com\Lib\Fun\Array_::toArray( $relationDataOfPermission['rbac_ids'], [
				'unique' => true,
				'sort' => 1,
			]), $permissionIds );
			//
			$saveObj = thisRelation\Sava::makeOfUpdate( $relationDataOfPermission['id'] );
			$saveObj->goWaitSaveDataManager()->newData_RbacIds()->setOfContinuousIds( $newPermissionIds );
			$saveObj->saveOfUpdate();
		}
		//
		return true;
	}

	/**
	 * 解绑PermissionIds
	 * @param array $permissionIds
	 * @return bool
	 */
	public function unBindPermissionIds( array $permissionIds ):bool {
		$relationDataOfRole = $this->dataStore->main->gowPermission()->goDirectDataManaer_New()
			->fieldData_Id()
			->fieldData_RbacIds()
			->findOrNullToArray();
		if( $relationDataOfRole !== null ){
			$saveObj = thisRelation\Sava::makeOfUpdate( $relationDataOfRole['id'] );
			$saveObj->goWaitSaveDataManager()->newData_RbacIds()->setOfContinuousIds( array_diff( $relationDataOfRole['rbac_ids'], $permissionIds ) );
			$saveObj->saveOfUpdate();
		}
		//
		return true;
	}

}
